//
//  CYChooseCouponViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/9.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYChooseCouponViewController.h"

#import "CYChooseCouponCell.h"



@interface CYChooseCouponViewController ()

@property(nonatomic, strong)UIView *bottomView;
@property(nonatomic, strong)UIButton *submitButton;

@property(nonatomic, strong)CYChooseCouponCellObject *currentCellObject;
@property(nonatomic, strong)NSMutableArray *cells;

@end


@implementation CYChooseCouponViewController

- (void)loadUI
{
    self.navigationItem.title = @"选择优惠券";
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 15)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 15)];
    
    self.bottomView = [[UIView alloc] init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bottomView];
    {
        self.submitButton = [UIButton cy_mainButtonWithTitle:@"确定"];
        [self.submitButton addTarget:self action:@selector(onSubmitButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:self.submitButton];
    }
}

- (void)loadData
{
    [self loadDataFromServer];
}

- (void)loadDataFromServer
{
    [SVProgressHUD show];
    @weakify(self);
    [CYStandardDataAPI project_queryUserCouponWithAmount:self.amount projectId:self.pid complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            [SVProgressHUD dismiss];
            if([response.content isKindOfClass:[NSArray class]])
            {
                NSArray *models = [CYDataAPIChooseCouponModel mj_objectArrayWithKeyValuesArray:response.content];
                NSMutableArray *cells = [NSMutableArray array];
                for(CYDataAPIChooseCouponModel *model in models)
                {
                    CYChooseCouponCellObject *cell = [CYChooseCouponCellObject objectWithCellClass:[CYChooseCouponCell class]];
                    cell.expandAction = ^{
                        @strongify(self);
                        [self.tableView reloadData];
                    };
                    cell.model = model;
                    if(self.currentCid && self.currentCid.integerValue != 0 &&
                       [self.currentCid isEqualToNumber:SAFE_NUMBER(model.iuserCouponId)] &&
                       SAFE_NUMBER(model.iUse).integerValue == 1)
                    {
                        cell.selected = YES;
                        self.currentCellObject = cell;
                    }
                    else
                    {
                        cell.selected = NO;
                    }
                    [cells addObject:cell];
                }
                self.cells = cells;
                self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:cells delegate:(id)[NICellFactory class]];
                self.tableView.dataSource = self.tableModel;
                [self.tableView reloadData];
            }
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:response.message];
        }
    }];
}

- (void)layoutUI
{
    self.tableView.width = self.contentView.width;
    self.tableView.height = self.contentView.height - 60;
    self.tableView.left = 0;
    self.tableView.top = 0;
    
    self.bottomView.width = self.contentView.width;
    self.bottomView.height = 60;
    self.bottomView.left = 0;
    self.bottomView.bottom = self.contentView.height;
    {
        self.submitButton.width = self.bottomView.width - 40;
        self.submitButton.height = 40;
        self.submitButton.centerX = self.bottomView.width / 2;
        self.submitButton.centerY = self.bottomView.height / 2;
    }
}

#pragma mark - Control Events
- (void)onSubmitButtonTouchUpInside
{
    if(self.complete)
    {
        self.complete(self.currentCellObject.model);
    }
    [self back];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    id cellObject = [self.tableModel objectAtIndexPath:indexPath];
    if(SAFE_NUMBER([(CYChooseCouponCellObject *)cellObject model].iUse).integerValue == 1)
    {
        self.currentCellObject.selected = NO;
        if(cellObject == self.currentCellObject)
        {
            self.currentCellObject = nil;
        }
        else
        {
            self.currentCellObject = cellObject;
            self.currentCellObject.selected = YES;
        }
    }
    [tableView reloadData];
}

@end
